# coding : utf-8
# @Author : 赐恩明
# vx : 17341788306


import time
import pytest
import pyttsx3
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from boss_robot.common.tools import Tool
from boss_robot.common.main import Run


class Test_Auto:
    tool = Tool()
    cfg = Run()
    number = int(cfg.number)
    index = int(cfg.index)
    voice = cfg.voice.lower()

    @classmethod
    def __webdrivewait_ele(cls, driver, timeout: float = 10, step: float = 0.5, *locator):
        element = WebDriverWait(driver, timeout, step).until(EC.visibility_of_element_located(locator))
        return element

    @classmethod
    def __webdrivewait_eles(cls, driver, timeout: float = 10, step: float = 0.5, *locator):
        elements = WebDriverWait(driver, timeout, step).until(EC.visibility_of_all_elements_located(locator))
        return elements

    @pytest.mark.parametrize('data', [('成都', number, index)])
    def test_inner(self, auto_boss, data):
        try:
            self.__webdrivewait_ele(auto_boss, 2, 0.5, By.CSS_SELECTOR,
                                    'div.dialog-wrap.dialog-account-safe>div.dialog-container>div>a').click()
        except:
            pass
        auto_boss.refresh()
        time.sleep(2)
        elements = self.__webdrivewait_eles(auto_boss, 10, 1, By.XPATH, "//li[starts-with(@ka,'target_position')]")
        if data[2] != 0:
            elements[int(data[2])].click()
        flag = True
        # auto_boss.find_element(By.XPATH, '//span[text()="最新职位"]').click()
        self.tool.cv2_click(r'.\image_s\img_1.png')
        while flag:
            auto_boss.implicitly_wait(10)
            eles = auto_boss.find_elements(By.CSS_SELECTOR, 'span.job-name')
            s = 100
            for ele in eles:
                if Run.n >= int(data[1]):
                    print(f'本次投递{Run.n}份简历。')
                    flag = False
                    break
                auto_boss.execute_script(f'window.scrollTo(0,{s});')
                s += 100
                time.sleep(1)
                auto_boss.execute_script('arguments[0].click();', ele)
                windows = auto_boss.window_handles
                auto_boss.switch_to.window(windows[1])
                self.__webdrivewait_ele(auto_boss, 10, 1, By.CSS_SELECTOR, "a.btn.btn-startchat")
                if self.tool.cv2_click(r'.\image_s\img.png'):
                    self.__webdrivewait_ele(auto_boss, 10, 1, By.CSS_SELECTOR, "textarea.input-area")\
                        .send_keys(self.cfg.greetings)
                    time.sleep(1)
                    self.tool.cv2_click(r'.\image_s\send.png')
                    Run.n += 1
                auto_boss.implicitly_wait(0)
                try:
                    if self.__webdrivewait_ele(auto_boss, 1, 0.5, By.XPATH, "//p[text()='今日沟通人数已达上限，请明天再试']"):
                        auto_boss.find_element(By.CSS_SELECTOR, 'span.btn.btn-sure').click()
                        flag = False
                        if self.voice == 'yes':
                            pyttsx3.speak('今日已达上限！')
                        print('今日沟通人数已达上限，请明天再试，今日投递' + f'{self.cfg.n}份简历。')
                        break
                except:
                    auto_boss.close()
                    auto_boss.switch_to.window(windows[0])
            if flag:
                next_page = self.__webdrivewait_ele(auto_boss, 1, 0.5, By.CSS_SELECTOR, "i.ui-icon-arrow-right")
                next_page.click()
